<?php

namespace Util;
class Image
{
    /**
     * pc端上传图片
     * @param $packet
     * @return bool
     */
    public static function pcUpload($packet)
    {
        if ($packet && $packet["type"]) {
            $path = "files/";
            if (!file_exists($path)) {
                mkdir($path, 0777);
            }
            $filetype = $packet['type'];
            if ($packet["name"]) {
                $today = $packet['name'];
                $object = $today;
                $file2 = $path . $object;
                $flag = 1;
            }
            $result = false;
            if ($flag) {
                $result = move_uploaded_file($packet["tmp_name"], $file2);
            }
            if ($result) {

                require_once dirname(__FILE__) . '/alioss/sdk.class.php';
                require_once dirname(__FILE__) . '/alioss/util/oss_util.class.php';
                $oss = new \ALIOSS(OSS_ACCESS_ID, OSS_ACCESS_KEY, OSS_ENDPOINT);
                //初始化
                $bucket = OSS_BUCKET_XQ;
                $file_path = __FILE__;
                $options = array(
                    \ALIOSS::OSS_HEADERS => array(
                        'Content-Type' => $filetype)
                );
                //                if ($type == '.jpg') {
                $res = $oss->upload_file_by_file($bucket, 'files/' . $object, $file2, $options);
                unlink($file2);
                if ($res->status == 200) {
                    return true;
                }
                return false;
            } else {
                return false;
            }
        }
    }

    /**
     * 记录日志
     * @param $file_name
     * @return int
     */
    public static function appUpload($img)
    {
        $file_name = self::guid() . '.jpg';
        require_once dirname(__FILE__) . '/alioss/sdk.class.php';
        require_once dirname(__FILE__) . '/alioss/util/oss_util.class.php';

        $oss = new \ALIOSS(OSS_ACCESS_ID, OSS_ACCESS_KEY, OSS_ENDPOINT);
        $bucket = OSS_BUCKET_XQ;

        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace('data:image/jpeg;base64,', '', $img);
        $img = str_replace('data:image/jpg;base64,', '', $img);

        $img = str_replace(' ', '+', $img);
        $content = base64_decode($img);

        $options = array(
            'content' => $content,
            'length' => strlen($content),
            \ALIOSS::OSS_HEADERS => array(
                'Content-Type' => 'image/pjpeg')
        );
        $res = $oss->upload_file_by_content($bucket, 'files/' . $file_name, $options);
        //var_dump($res);exit;
        //$msg = "上传本地到 /" . $bucket . "/" . $file_name;
        //OSSUtil::print_res($res, $msg);
        if ($res->isOk()) {
            return 'files/' . $file_name;
        }
        return '';
    }

    private static function guid()
    {
        mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = substr($charid, 0, 8) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 12);
        return $uuid;
    }

    /**
     * 去除图片路径
     * @param $image_url
     * @return mixed|string
     */
    public static function dealImageUrl($image_url)
    {
        if (empty($image_url)) {
            return '';
        }
        $image_domain = \Util\Config::get('image_url', 'Common');
        if (strpos($image_url, $image_domain) !== false) {
            $image_url_temp = str_replace($image_domain, '', $image_url);
        } elseif (strpos($image_url, 'http://thirdwx.qlogo.cn') !== false) {
            $image_url_temp = $image_url;
        } else {
            $image_url_files = str_replace('files/', '', $image_url);
            $image_url_temp = 'files/' . $image_url_files;
        };
        return $image_url_temp;
    }

    /**
     * 处理微信错误头像
     * @param $image_url
     * @return mixed|string
     */
    public static function dealWeixinPortrait($image_url)
    {
        if(empty($image_url)){
            return  '/img/wode_moren.png';
        }

        $image_domain = \Util\Config::get('image_url', 'Common');
        if ($image_url == $image_domain || $image_url == 'files/' || $image_url == $image_domain . 'files/') {
            $image_url_temp = '/img/touxiang.png';
        } elseif (strpos($image_url, 'http://thirdwx.qlogo.cn') !== false) {
            $image_url_temp = $image_url;
        } else {
            $image_url_temp = $image_domain . self::dealImageUrl($image_url);
        }
        return $image_url_temp;
    }
}